package csel.view;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import csel.model.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.LinkedList;

/*
 * Reasons for Existence: Encapsulates the part of the status viewport that displays the 
 * player's equipment contents. Provides the controller information about user interaction 
 * with the equipment section of the status viewport. This is a distinct function and should 
 * be differentiated from the other features of the StatusViewport.
 * 
 * @author Alex
 */

public class EquipmentViewer extends JPanel implements Observer
{
	private static final long serialVersionUID = 1L;

	ButtonGroup itemButtonGroup;
	LinkedList<JToggleButton> itemButtons;
	JButton unequip;

	JPanel buttonPane;
	JPanel itemsPane;
	JPanel groupPane;

	JToggleButton lb;

	public EquipmentViewer(Entity e, ActionListener a)
	{
		e.addObserver(this);

		buttonPane = new JPanel();
		itemsPane = new JPanel();
		groupPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane,BoxLayout.X_AXIS));

		unequip = new JButton("Unequip");
		unequip.setActionCommand("stunequip");
		unequip.addActionListener(a);
		buttonPane.add(unequip);

		buttonPane.setOpaque(false);
		
		unequip.setFocusable(false);

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		//		buttonPane.setOpaque(false);

		groupPane.setLayout(new GridBagLayout());
		groupPane.add(buttonPane,c);
		groupPane.setOpaque(false);

		add(groupPane);

		modelUpdate(e);
	}

	public EquippableItem[] getSelectedItems()
	{
		LinkedList<EquippableItem> selectedItems = new LinkedList<EquippableItem>();

		//if there aren't any buttons, skip this
		int size = itemButtons.size();
		if (size != 0)
		{
			//gets all selected buttons
			for (int a = 0; a < size; ++a)
			{
				if (itemButtons.get(a).getSelectedObjects() != null)
				{
					selectedItems.add((EquippableItem) itemButtons.get(a)
							.getClientProperty("myItem"));
					lb = itemButtons.get(a);
				}
			}

			//builds a list of their items
			Object[] itemList = selectedItems.toArray();
			int itemsLength = itemList.length;
			EquippableItem[] takeables = new EquippableItem[itemsLength];

			//returns them
			for (int a = 0; a < itemsLength; ++a)
				takeables[a] = (EquippableItem) itemList[a];
			return takeables;
		} 
		else
			return null;
	}

	public void setEntity(Entity e)
	{
		e.addObserver(this);
		modelUpdate(e);
	}

	public void modelUpdate(Object o)
	{
		//		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

		Entity entity = ((Entity) o);
		Dictionary<String,EquippableItem> eItems = entity.getEquippedItems();
		int size = eItems.size();
		if(entity.getInventory().hasChanged())
		{
			JPanel titemsPane = new JPanel();
			titemsPane.setLayout(new GridBagLayout());
			//		titemsPane.setOpaque(false);
			GridBagConstraints c = new GridBagConstraints();
			int rowNum = 1;

			//gets all of the provided Entity's equipped items
			EquippableItem[] eList = new EquippableItem[size];
			int a = 0;
			for (Enumeration<EquippableItem> eItemsEnumeration = eItems.elements() ; eItemsEnumeration.hasMoreElements() ;) {
				eList[a] = (EquippableItem) eItemsEnumeration.nextElement();
				++a;
			}
			itemButtonGroup = new ButtonGroup();
			itemButtons = new LinkedList<JToggleButton>();

			//builds a button for each equipped item
			for (int b = 0; b < size; ++b)
			{
				JToggleButton j = new JToggleButton(new ImageIcon(RepresentationFactory.getInstance().get(eList[b]).getSimpleImage()));
				j.putClientProperty("myItem", eList[b]);
				j.setPreferredSize(new Dimension(50,50));
				j.setFocusable(false);
				itemButtonGroup.add(j);
				itemButtons.add(j);

				c.gridx = 0;
				c.gridy = rowNum;
				titemsPane.add(j,c);
				c.gridx = 1;
				c.gridy = rowNum++;
				JLabel item = new JLabel(eList[b].getDescription());
				item.setForeground(Color.WHITE);
				titemsPane.add(item ,c);
			}


			titemsPane.setOpaque(false);
			c.gridx = 0;
			c.gridy = 1;
			groupPane.remove(itemsPane);
			itemsPane = titemsPane;
			groupPane.add(itemsPane,c);
			groupPane.revalidate();
		}
	}


	
	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
	{
		throw new java.io.NotSerializableException("AvatarViewer should not be serialized.");
	}
	
	private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
	{
		throw new java.io.NotSerializableException("AvatarViewer should not be serialized.");
	}
}
